package com.zzdx.deployment.web.controller.msg;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.UnsupportedEncodingException;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author wenhan(Mecera)
 * @version V1.0
 * @Title: MsgController
 * @Description: TODO(用一句话描述该文件做什么)
 * @date 12:27 AM
 */
@Controller
@RequestMapping("/msg")
public class MsgController {

    /**
     * 发送短信
     */
//    @RequiresPermissions("system:msg:msg")
    @PostMapping("/sendMsg")
    @ResponseBody
    public String sendMsg(String phones,String mhtMsgIds,String msgType,String msgContent) throws UnsupportedEncodingException {
        String url = "http://120.55.193.51:8098/smtp/http/submit";
        String userName = "cbszwfw";
        String userPassword = "cbszwfw@123";
        String serviceCode = "cbszwfw";
        String timestamp = getTime();
        String sendTime = "";
        int priority = 5;
        String orgCode = "";
        String extSubNum  = "";
        String clientStr = new String(msgContent.getBytes("UTF8"), "GBK");
        int reportFlag = 0;
        String sign = md5(userPassword+timestamp).toUpperCase();
        String params = "timestamp="+timestamp+"&userName="+userName+"&sign="+sign+
                "&serviceCode="+serviceCode+"&phones="+phones+"&mhtMsgIds="+mhtMsgIds
                +"&sendTime="+sendTime+"&priority="+priority+"&orgCode="+orgCode
                +"&msgType="+msgType+"&clientStr="+clientStr+"&reportFlag="+reportFlag;
        String bak = HttpRequest.sendPost(url,params);
        return bak;
    }



    public static String md5(String str) {
        try {
            // 生成一个MD5加密计算摘要
            MessageDigest md = MessageDigest.getInstance("MD5");
            // 计算md5函数
            md.update(str.getBytes());
            // digest()最后确定返回md5 hash值，返回值为8为字符串。因为md5 hash值是16位的hex值，实际上就是8位的字符
            // BigInteger函数则将8位的字符串转换成16位hex值，用字符串来表示；得到字符串形式的hash值
            return new BigInteger(1, md.digest()).toString(16);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }

    public String getTime() {
        Date date = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmss");
        String formatTime = formatter.format(date);
        return formatTime;
    }
}